﻿using Cxuu.Common;
using Cxuu.Models;
using PetaPoco;
using System.Threading.Tasks;

namespace Cxuu.Repository
{
    [AppService]
    public class ArticleDocreceivedRepository : BaseRepository<ArticleDocreceived, PetaPocoContext>
    {

        public ArticleDocreceivedRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }

        /// <summary>
        /// 编辑使用 查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EditArticleDocreceived> GetEditAsync(int? id)
        {
            Sql sql = Sql.Builder;
            sql.Select("article.*,cate.Name as CateName")
                .From("cxuu_article_docreceived as article")
                .LeftJoin("cxuu_article_cate as cate").On("article.Cid = cate.Id");
            sql.Where("article.Id =@0", id);
            return await CreateDAO().FirstOrDefaultAsync<EditArticleDocreceived>(sql);
        }


        /// <summary>
        /// 根据CID 查询一条 用于判断当前栏目下有没有数据及查询最新一条信息内容
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public async Task<ArticleDocreceived> GetArticleByCidAsync(int cid)
        {
            Sql sql = Sql.Builder;
            sql.Select("*").From("cxuu_article_docreceived").Where("Cid =@0", cid).OrderBy("Id desc");
            return await CreateDAO().FirstOrDefaultAsync<ArticleDocreceived>(sql);
        }

        /// <summary>
        /// 获取列表 
        /// </summary>
        public async Task<Page<ViewArticleDocreceivedAndCate>> GetListAsync(ArticleDocreceivedQuery articleQuery)
        {
            Sql sql = Sql.Builder;
            sql.Select("cate.Name as CateName,dept.DeptName,u.Nickname,article.*")
                .From("cxuu_article_docreceived as article")
                .LeftJoin("cxuu_article_cate as cate").On("article.Cid = cate.Id");
            sql.LeftJoin("cxuu_admin_user u").On("article.Uid=u.Id");
            sql.LeftJoin("cxuu_admin_dept dept").On("u.DeptId=dept.Id");

            if (articleQuery.Miji > -1)
                sql.Where("article.Miji = @0", articleQuery.Miji);

            if (articleQuery.Uids == "own")
                sql.Where("article.Uid = @0", articleQuery.Uid);
            else if (articleQuery.Uids != null)
                sql.Where("article.Uid IN (" + articleQuery.Uids + ")");

            if (articleQuery.Cid != "0" && articleQuery.Cid != null)
                sql.Where("article.Cid =@0", int.Parse(articleQuery.Cid));

            if (!string.IsNullOrEmpty(articleQuery.Title))
                sql.Where("article.Title like @0", "%" + articleQuery.Title + "%");
            
            if (!string.IsNullOrEmpty(articleQuery.Clercator))
                sql.Where("article.Clercator like @0", "%" + articleQuery.Clercator + "%");

            if (!string.IsNullOrEmpty(articleQuery.DocNumber))
                sql.Where("article.DocNumber like @0", "%" + articleQuery.DocNumber + "%");

            if (!string.IsNullOrEmpty(articleQuery.Orderby))
                sql.OrderBy("article." + articleQuery.Orderby);

            return await CreateDAO().PageAsync<ViewArticleDocreceivedAndCate>(articleQuery.CurrentPage, articleQuery.Limit, sql);
        }


        /// <summary>
        /// 插入一条信息
        /// </summary>
        /// <param name="article"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task<ArticleDocreceived> InsertArticleAndContentAsync(ArticleDocreceived  articleDocreceived)
        {
            var dao = CreateDAO();
            await dao.OpenSharedConnectionAsync();
            try
            {
                var a = await dao.InsertAsync(articleDocreceived);

                if (a != null)
                    return articleDocreceived;
            }
            finally
            {
                dao.CloseSharedConnection();
            }
            return null;
        }
        /// <summary>
        /// 更新一条信息
        /// </summary>
        /// <param name="article"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task<int> UpdateArticleAndContentAsync(EditArticleDocreceived articleDocreceived)
        {
            if (articleDocreceived != null)
            {
                var dao = CreateDAO();
                ArticleDocreceived a = await dao.SingleOrDefaultAsync<ArticleDocreceived>(articleDocreceived.Id);
                a.Cid = articleDocreceived.Cid;
                a.Title = articleDocreceived.Title;
                a.Attments = articleDocreceived.Attments;
                a.Miji = articleDocreceived.Miji;
                a.GreatTime = articleDocreceived.GreatTime;
                a.DocNumber = articleDocreceived.DocNumber;
                a.Department = articleDocreceived.Department;
                a.Clercator = articleDocreceived.Clercator;
                a.Remarks = articleDocreceived.Remarks;
                return await dao.UpdateAsync(a);
            }
            return 0;
        }
    }
}
